<?php


namespace app\common\business;


use app\common\lib\SetArray;

class Category
{
    public $categoryModel = null;

    public function __construct() {
        //初始化模型对象
        $this->categoryModel = new \app\common\model\mysql\Category();
    }

    public function getListTree() {
        $res = $this->categoryModel->getNormalList();
        if (empty($res)) {
            return [];
        }
        $data = $res->toArray();
        $tree = SetArray::childrenAfterParent2($data);
        return $tree;
    }

    public function getAllNormalList() {
        $data = $this->categoryModel->getAllNormalList();
        //排序   子分类拍在父类后面
        $newData = SetArray::childrenAfterParent3($data);
        return $newData;
    }

    public function getById($id) {
        $result = $this->categoryModel->find($id);
        if(empty($result)) {
            return [];
        }
        $result = $result->toArray();
        return $result;
    }

    public function listorder($id, $listorder) {
        // 查询 id这条数据是否存在
        $res = $this->getById($id);
        if(!$res) {
            throw new \think\Exception("不存在该条记录");
        }
        $data = [
            "list_order" => $listorder,
        ];

        try {
            //$this->model->where(["id" => $id])->save($data);
            $res = $this->categoryModel->updateById($id, $data);
        }catch (\Exception $e) {
            // 记得记录日志。
            return false;
        }
        return $res;
    }

    public function status($id, $status) {
        // 查询 id这条数据是否存在
        $res = $this->getById($id);
        if(!$res) {
            throw new \think\Exception("不存在该条记录");
        }
        if($res['status'] == $status) {
            throw new \think\Exception("状态修改前和修改后一样没有任何意义哦");
        }

        $data = [
            "status" => intval($status),
        ];

        try {
            $res = $this->categoryModel->updateById($id, $data);
        }catch (\Exception $e) {
            // 记得记录日志。
            return false;
        }
        return $res;
    }
    public function getchildrenByPid($pid) {
        // 查询 id这条数据是否存在
        $res = $this->getById($pid);
        if(!$res) {
            throw new \think\Exception("不存在该条记录");
        }
        try {
            $res = $this->categoryModel->getchildrenByPid($pid);
        }catch (\Exception $e) {
            // 记得记录日志。
            return false;
        }
        return $res;
    }

    public function getMenusAndGoods() {
        //获取menu的分类ID
        $order = ["list_order" => "desc", "id" => "desc"];
        $res = $this->categoryModel->field('id, cat_name')->where('type', 1)->order($order)->select();
        if (!$res) {
            return [];
        }
        $categorys = $res->toArray();

        foreach ($categorys as $k=>$v) {
            $cats = $this->categoryModel->getChildrenIds($v['id']);
            array_push($cats, $v['id']);
            $res = (new \app\common\model\mysql\Goods())->getGoodsByCategoryIds($cats, 5);
            if ($res) {
                $categorys[$k]['goods'] = $res->toArray();
            } else {
                $categorys[$k]['goods'] = [];
            }
        }
        return $categorys;
    }

    /**
     * 获取顶级分类及商品
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getFirstCategoryAndGoods() {
        //获取顶级分类
        $data = $this->categoryModel->getFirstList(10);
        if (empty($data)) {
            return [];
        }
        foreach ($data as $k=>$v) {
            $res = $this->categoryModel->getChildrenIds($v['id']);
            array_push($res, $v['id']);
            $goods = (new \app\common\model\mysql\Goods())->getGoodsByCategoryIds($res, 24);
            $data[$k]['goods'] = $goods;
        }
        return $data;
    }


}